'use strict';

import React, {Component} from 'react';
import PropTypes from 'prop-types';
import {withTranslation} from '../lib/i18n';
import {requiresAuthenticatedUser, Title, withPageHelpers} from '../lib/page';
import {Table} from '../lib/table';
import {HTTPMethod} from '../lib/axios';
import {withComponentMixins} from "../lib/decorator-helpers";
import {tableAddRestActionButton, tableRestActionDialogInit, tableRestActionDialogRender} from "../lib/modals";

@withComponentMixins([
    withTranslation,
    withPageHelpers,
    requiresAuthenticatedUser
])
export default class UserShares extends Component {
    constructor(props) {
        super(props);

        this.sharesTables = {};

        this.state = {};
        tableRestActionDialogInit(this);
    }

    static propTypes = {
        user: PropTypes.object
    }

    render() {
        const t = this.props.t;

        const renderSharesTable = (entityTypeId, title, typeName) => {
            const columns = [
                { data: 0, title: t('name') },
                { data: 1, title: t('role') },
                {
                    actions: data => {
                        const actions = [];
                        const autoGenerated = data[3];
                        const perms = data[4];

                        if (!autoGenerated && perms.includes('share')) {
                            const name = data[0];
                            const entityId = data[2];

                            tableAddRestActionButton(
                                actions,
                                this,
                                {
                                    method: HTTPMethod.PUT,
                                    url: 'rest/shares',
                                    data: {
                                        entityTypeId,
                                        entityId,
                                        userId: this.props.user.id
                                    },
                                    refreshTables: () => {
                                        for (const key in this.sharesTables) {
                                            this.sharesTables[key].refresh();
                                        }
                                    }
                                },
                                { icon: 'trash-alt', label: t('unshare') },
                                t('confirmUnsharing'),
                                t('areYouSureYouWantToRemoveTheSharingOfThe', {typeName, name}),
                                t('removingSharingOfTheTypeNameName', {typeName, name}),
                                t('sharingOfTheTypeNameNameRemoved', {typeName, name}),
                                null
                            );
                        }

                        return actions;
                    }
                }
            ];

            return (
                <div>
                    <h3>{title}</h3>
                    <Table ref={node => this.sharesTables[entityTypeId] = node} withHeader dataUrl={`rest/shares-table-by-user/${entityTypeId}/${this.props.user.id}`} columns={columns} />
                </div>
            );
        };

        return (
            <div>
                {tableRestActionDialogRender(this)}
                <Title>{t('sharesForUserUsername', {username: this.props.user.username})}</Title>

                {renderSharesTable('namespace', t('namespaces'), t('namespace-1'))}
                {renderSharesTable('list', t('lists'), t('list-1'))}
                {renderSharesTable('template', t('templates'), t('template-1'))}
                {renderSharesTable('mosaicoTemplate', t('mosaicoTemplates'), t('mosaicoTemplate'))}
                {renderSharesTable('campaign', t('campaigns'), t('campaign-1'))}
                {renderSharesTable('customForm', t('customForms-1', t('customForms-2')))}
                {renderSharesTable('report', t('reports'), t('report-1'))}
                {renderSharesTable('reportTemplate', t('reportTemplates'), t('reportTemplate-2'))}
                {renderSharesTable('sendConfiguration', t('sendConfigurations-1'), t('sendConfiguration'))}
            </div>
        );
    }
}
